local skel = fk.CreateSkill {
  name = "efengqi__zuilun",
}

Fk:loadTranslationTable{
  ["efengqi__zuilun"] = "罪论",
  [":efengqi__zuilun"] = "结束阶段，你可卜算3，你每满足以下一项便摸一张牌：1.你于此回合内造成过伤害；2.你于此回合内未弃置过牌；3.手牌数为全场最少。若均不满足，你视为使用一张【决斗】。",

  ["#efengqi__zuilun-invoke"] = "罪论：你可以卜算3，然后摸 %arg 张牌",
  ["#efengqi__zuilun-duel"] = "罪论：你可以卜算3，然后使用【决斗】！",
  ["#efengqi__zuilun-choose"] = "罪论：选择一名其他角色，你与其各失去1点体力",

  ["$efengqi__zuilun1"] = "吾有三罪，未能除黄皓、制伯约、守国土。",
  ["$efengqi__zuilun2"] = "唉，数罪当论，吾愧对先帝恩惠。",
}

skel:addEffect(fk.EventPhaseStart, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return target == player and target:hasSkill(skel.name) and player.phase == Player.Finish
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local n = 0
    if #room.logic:getActualDamageEvents(1, function (e)
      return e.data.from == player
    end, Player.HistoryTurn) > 0 then
      n = n + 1
    end
    if #room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
      for _, move in ipairs(e.data) do
        if move.from == player and move.moveReason == fk.ReasonDiscard
         and table.find(move.moveInfo, function (info)
          return info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip
        end) then
          return true
        end
      end
    end, Player.HistoryTurn) == 0 then
      n = n + 1
    end
    if table.every(room.alive_players, function(p) return p:getHandcardNum() >= player:getHandcardNum() end) then
      n = n + 1
    end
    local prompt = n > 0 and "#efengqi__zuilun-invoke:::"..n or "#efengqi__zuilun-duel"
    if room:askToSkillInvoke(player, { skill_name = self.name, prompt = prompt }) then
      event:setCostData(self, {n = n})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local n = event:getCostData(self).n
    room:askToGuanxing(player, { cards = room:getNCards(3), skill_name = skel.name })
    if n > 0 then
      player:drawCards(n, self.name)
    else
      room:askToUseVirtualCard(player, {
        name = "duel", skill_name = skel.name, cancelable = false, skip = false,
      })
    end
  end,
})

return skel
